/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import ${appConfig.outputPackageName}.web.view.FlexXMLView;
import ${appConfig.outputPackageName}.controller.ServerProcessStatus;
import ${appConfig.outputPackageName}.controller.ActionBean;
import ${appConfig.outputPackageName}.trans.TransactionProxy;
import ${appConfig.outputPackageName}.model.${entity.javaName};

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class  ${entity.javaName}UpdateController extends AbstractController 
{
	private static Logger log = LoggerFactory.getLogger(${entity.javaName}UpdateController.class);
	private TransactionProxy transactionProxy;	
	
	
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
	HttpServletResponse response) throws Exception 
	{
		if(log.isDebugEnabled()) log.debug("Entering ${entity.javaName}UpdateController ");		
		ActionBean action = new ActionBean();
		BeanUtils.populate(action, request.getParameterMap());
		${entity.javaName} commandObject = new ${entity.javaName}();
		ServerProcessStatus status = new ServerProcessStatus();		
		BeanUtils.populate(commandObject, request.getParameterMap());		
		status.setAction(action.getAction());
		try{
			commandObject =  getTransactionProxy().update${entity.javaName}(commandObject);		
			if(commandObject!=null){
				status.setMessage("updated ${entity.javaName} in database successfully.");
				status.setOutput(commandObject);
			}else{
				status.setMessage("failed to update ${entity.javaName} to database .");
			}
			
		}catch(Exception ex){
			log.error("Failed to execute add()", ex);
			status.setError(ex);
			status.setMessage("failed to update ${entity.javaName} to database .");
		}	
		String viewName = getViewName();
		if(action.isXmlView()){
			viewName = FlexXMLView.NAME;
		}		
		ModelAndView mv = new ModelAndView(viewName);
		mv.addObject(ServerProcessStatus.SERVER_PROCESS_STATUS, status);
		if(log.isDebugEnabled()) log.debug("Exiting ${entity.javaName}UpdateController ");		
		return mv;
	}
	
	public TransactionProxy getTransactionProxy()
	{
		return this.transactionProxy;
	}
	
	public void setTransactionProxy(TransactionProxy proxy)
	{
		this.transactionProxy = proxy;
	}
	
	protected String getViewName()
	{
		return "${entity.javaName}DetailView";
	}
	
}